package com.atguigu.gmall.product.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-04-21 10:33
 */
@RestController
@RequestMapping("/api/product")
public class ProductApiController {

    @Autowired
    private SkuManageService skuManageService;

    @Autowired
    private SpuManageService spuManageService;

    @Autowired
    private BaseCategoryViewService baseCategoryViewService;

    @Autowired
    private BaseAttrInfoService baseAttrInfoService;

    @Autowired
    private BaseTrademarkService baseTrademarkService;

    /**
     * 获取商品信息 包含商品图片
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable("skuId") Long skuId, @RequestHeader(value = "testheader", required = false) String testHeader) {
        System.out.println("获取到头信息:" + testHeader);
        SkuInfo skuInfo = skuManageService.getSkuInfoFromDB(skuId);
        return skuInfo;
    }

    /**
     * 根据三级分类ID 查询所属所有三级分类信息
     *
     * @param category3Id
     * @return
     */
    @GetMapping("/inner/getCategoryView/{category3Id}")
    @GmallCache(prefix = "baseCategoryView:")
    public BaseCategoryView getCategoryView(@PathVariable("category3Id") Long category3Id) {
        BaseCategoryView baseCategoryView = baseCategoryViewService.getById(category3Id);
        return baseCategoryView;
    }


    /**
     * 根据skuID查询商品价格
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getSkuPrice/{skuId}")
    public BigDecimal getSkuPrice(@PathVariable("skuId") Long skuId) {
        BigDecimal priceBigDecimal = skuManageService.getSkuPrice(skuId);
        return priceBigDecimal;
    }


    /**
     * 根据SPUID查询商品所有海报列表
     *
     * @param spuId
     * @return
     */
    @GetMapping("/inner/findSpuPosterBySpuId/{spuId}")
    public List<SpuPoster> getSpuPosterBySpuId(@PathVariable("spuId") Long spuId) {
        List<SpuPoster> spuPosterList = spuManageService.getSpuPosterBySpuId(spuId);
        return spuPosterList;
    }

    /**
     * 根据商品SkuID查询当前sku商品所有平台属性名称包含属性值
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getAttrList/{skuId}")
    public List<BaseAttrInfo> getAttrList(@PathVariable("skuId") Long skuId) {
        List<BaseAttrInfo> list = baseAttrInfoService.getAttrList(skuId);
        return list;
    }

    /**
     * 查询SPU所有销售属性,将指定传入SKU商品销售属性区分是否选中
     *
     * @param skuId
     * @param spuId
     * @return
     */
    @GetMapping("/inner/getSpuSaleAttrListCheckBySku/{skuId}/{spuId}")
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(
            @PathVariable("skuId") Long skuId,
            @PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> list = spuManageService.getSpuSaleAttrListCheckBySku(skuId, spuId);
        return list;
    }


    /**
     * 查询每个商品SKU 销售属性 跟 SKUID 对照关系
     *
     * @param spuId
     * @return {"3732|3734":24,"3732|3735":25}
     */
    @GetMapping("/inner/getSkuValueIdsMap/{spuId}")
    public Map getSkuValueIdsMap(@PathVariable("spuId") Long spuId) {
        Map<String, String> valueJsonMap = spuManageService.getSkuValueIdsMap(spuId);
        return valueJsonMap;
    }


    /**
     * 查询所有一,二,三级分类
     *
     * @return
     */
    @GetMapping("/inner/getBaseCategoryList")
    public Result getBaseCategoryList() {
        List<JSONObject> list = baseCategoryViewService.getBaseCategoryList();
        return Result.ok(list);
    }

    /**
     * 根据品牌ID 查询 品牌对象
     *
     * @param tmId
     * @return
     */
    @GetMapping("/inner/getTrademark/{tmId}")
    public BaseTrademark getTrademark(@PathVariable("tmId") Long tmId) {
        return baseTrademarkService.getById(tmId);
    }
}
